<!DOCTYPE html>
<html class="ui-page-login">

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link href="css/mui.min.css" rel="stylesheet" />
		<link href="css/login.css" rel="stylesheet" />
		<style type="text/css">
			.mui-input-group label {
				width: 40%;
				text-align: right;
			}
			
			.mui-input-row label~input, .mui-input-row label~select, .mui-input-row label~textarea {
				width: 60%;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">服务器配置</h1>
		</header>
		<div class="mui-content">
			<form id='login-form' class="mui-input-group">
				<div class="mui-input-row">
					<label>服务器地址</label>
					<input id='serverip' type="text" class="mui-input-clear mui-input" placeholder="222.212.92.4" >
				</div>
				<div class="mui-input-row">
					<label>服务器端口</label>
					<input id='serverport' type="text" class="mui-input-clear mui-input" placeholder="10281" >
				</div>
				<div class="mui-input-row">
					<label>IM端口</label>
					<input id='import' type="text" class="mui-input-clear mui-input" placeholder="8611" >
				</div>
			</form>
			<div class="mui-content-padded">
				<button id='btnSave' class="mui-btn mui-btn-block mui-btn-primary">保 存</button>
			</div>
		</div>

	</body>

</html>
<script src="js/mui.min.js"></script>
<script src="js/app.js"></script>
<script src="js/config.js"></script>
<script src="js/server.js"></script>
<script type="text/javascript">
	var serverinfo = 'http://';
	var iminfo = 'ws://';
	var addr, port, imPort;

	var serverInfor = server.serverconfig.getConfigInfo();
	if(serverInfor) { //判断本地是否存在服务器配置信息
		document.getElementById('serverip').value = serverInfor.addr;
		document.getElementById('serverport').value = serverInfor.port;
		document.getElementById('import').value = serverInfor.import;
	}

	mui('body').on('tap', '#btnSave', function() {
		var isInput = checkInput();
		if(isInput) {
			var configobj = {
				'server': serverinfo,
				'imserver': iminfo,
				'addr': addr,
				'port': port,
				'import': imPort
			}
			var page = plus.webview.getWebviewById('login.html');
			mui.fire(page, 'myfresh');
			server.serverconfig.setConfigInfo(configobj);
			mui.toast('配置成功，请手动重启应用', {
				icon: 1
			});
			setTimeout(function() {
				plus.runtime.quit();
			}, 500);
			//window.location.href = 'login.html';
		}
	});

	// 检测文本框数据是否为空
	function checkInput() {
		addr = document.getElementById('serverip').value;
		port = document.getElementById('serverport').value
		imPort = document.getElementById('import').value;
		if(addr == '') {
			mui.toast("请输入服务器地址");
			return false;
		}
		if(port == '') {
			mui.toast("请输入服务器端口");
			return false;
		}
		if(imPort == '') {
			mui.toast("请输入IM端口");
			return false;
		}

		serverinfo += addr + ':' + port;
		iminfo += addr + ':' + imPort;
		return true;
	};

</script>